package de.gwtprime.client.widgets;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextBoxBase;

/**
 * Text boxes extended with placeholder functionality
 * @author jan
 *
 */
public class AdvancedTextBox extends TextBox{

	private String placeHolder = "";

	public AdvancedTextBox(){
		super();

		addClickHandler((new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				TextBoxBase source = (TextBoxBase)event.getSource();

				if(!source.getValue().equals(getPlaceHolder())){
					return;
				}


				source.setValue("");

			}
		}));


		addBlurHandler(new BlurHandler() {

			@Override
			public void onBlur(BlurEvent event) {
				TextBoxBase source = (TextBoxBase)event.getSource();

				// if user already typed something -> don't set back to default.
				if(!source.getValue().equals("")) return;

				source.setValue(placeHolder);

			}
		});
	}

	public String getPlaceHolder() {
		return placeHolder;
	}

	public void setPlaceHolder(String placeHolder) {
		setText(placeHolder);
		this.placeHolder = placeHolder;
	}

}
